/*
 * Created on 15.06.2003
 *
 * To change the template for this generated file go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
package pho.prowon.index;


import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
 * @author Peter
 *
 * To change the template for this generated type comment go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
public class IndexXMLImplementationSAX extends IndexXMLImplementation implements IndexImplementation
{
	/**
	 * @author Peter
	 *
	 * To change the template for this generated type comment go to
	 * Window>Preferences>Java>Code Generation>Code and Comments
	 */
	private class IndexReader extends DefaultHandler
	{
		// 0 .. not started
		// 1 .. Document started
		// 2 .. Image Data started
		// 3 .. FilePath started
		// 4 .. Tag started
		// 5 .. Value started
		private static final int STATUS_NO_DOCUMENT = 0;
		private static final int STATUS_DOCUMENT_STARTED = 1;
		private static final int STATUS_IMAGE_DATA_STARTED = 2;
		private static final int STATUS_FILEPATH_STARTED = 3;
		private static final int STATUS_TAG_STARTED = 4;
		private static final int STATUS_VALUE_STARTED = 5;
		private static final int STATUS_IMAGE_DATA_STARTED_WITH_PATH = 6;
		
		private int docStatus = STATUS_NO_DOCUMENT;

		private IndexReader ()
		{
		}

		/* (non-Javadoc)
		 * @see org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
		 */
		public void startElement(
			String uri,
			String localName,
			String qName,
			Attributes attributes)
			throws SAXException
		{
			super.startElement(uri, localName, qName, attributes);
			
			switch (docStatus)
			{
				case STATUS_DOCUMENT_STARTED:
					if (qName.equals(getImageElement()))
					{
						docStatus = STATUS_IMAGE_DATA_STARTED;
					}
					else
					{
						// PROBLEM: Invalid Image Data element
						System.out.println ("Found Element <"+qName+"> - expected: <"+getImageElement());
					}
					break;
				
				case STATUS_FILEPATH_STARTED:
					// PROBLEM: Child Element in Filepath
					System.out.println ("Found Element <"+qName+"> - as child of <FilePath>");
					break;
					
				case STATUS_IMAGE_DATA_STARTED:
					if (qName.equals("FilePath"))
					{
						docStatus = STATUS_FILEPATH_STARTED;
					}
					else
					{
						// PROBLEM: No File Path
						System.out.println ("Found Element <"+qName+"> - expected: <FilePath>");
					}
					break;

				case STATUS_IMAGE_DATA_STARTED_WITH_PATH:
					docStatus = STATUS_TAG_STARTED;
					currentTag = qName;
					break;

				case STATUS_TAG_STARTED:
					if (qName.equals("Value"))
					{
						docStatus = STATUS_VALUE_STARTED;
					}
					else
					{
						// PROBLEM: Invalid Value Element
						System.out.println ("Found Element <"+qName+"> - expected: <Value>");
					}
					break;

				case STATUS_VALUE_STARTED:
					System.out.println ("Found Element <"+qName+"> as child of <Value>");
					break;
				
				case STATUS_NO_DOCUMENT:
					if (qName.equals(getRootElement()))
					{
						docStatus = STATUS_DOCUMENT_STARTED;
					}
					else
					{
						// PROBLEM: Invalid Root element
						System.out.println ("Found Element <"+qName+"> - expected: <"+getRootElement()+">");
					}
					break;
				default:
					System.out.println ("Something wrong in the Index");
					break;
					
			}			
		}

		/* (non-Javadoc)
		 * @see org.xml.sax.ContentHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
		 */
		public void endElement(String uri, String localName, String qName)
			throws SAXException
		{
			super.endElement(uri, localName, qName);

			switch (docStatus)
			{
				case STATUS_FILEPATH_STARTED:
					if (qName.equals("FilePath"))
					{
						processNewImage ();
						docStatus = STATUS_IMAGE_DATA_STARTED_WITH_PATH;
					}
					break;
					
				case STATUS_IMAGE_DATA_STARTED_WITH_PATH:
					if (qName.equals("ImageData"))
					{
						processEndImage ();
						docStatus = STATUS_DOCUMENT_STARTED;
					}
					break;

				case STATUS_TAG_STARTED:
					if (qName.equals(currentTag))
					{
						processValue ();
						processEndTag ();
						docStatus = STATUS_IMAGE_DATA_STARTED_WITH_PATH;
						currentTag = "";
					}						
					break;

				case STATUS_VALUE_STARTED:
					if (qName.equals("Value"))
					{
						processValue();
						docStatus = STATUS_TAG_STARTED;
					}
					break;
				default:				
					break;
			}
			currentTagContent.setLength (0);
		}

		
		/* (non-Javadoc)
		 * @see org.xml.sax.ContentHandler#characters(char[], int, int)
		 */
		public void characters(char[] ch, int start, int length)
			throws SAXException
		{
			super.characters(ch, start, length);
			currentTagContent.append(ch,start,length);
		}
	}
	
	protected List readIndex (InputStream is) 
	{
		SAXParserFactory factory = SAXParserFactory.newInstance();
		SAXParser saxParser;
		IndexReader reader = new IndexReader();
		try
		{
			saxParser = factory.newSAXParser();
			saxParser.parse( is, reader );
		}
		catch (ParserConfigurationException e)
		{
			System.out.println("The underlying parser does not support the requested features.");
		}
		catch (SAXException e)
		{
			System.err.println(e);
		} 
		catch (IOException e)
		{
			System.err.println(e);
		}
		return imageList;
	}	

}
